import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:share_fit/app/controllers/base/base_sports_controller.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/routes/sports_routes.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'package:share_fit/app/ute/uteped_device.dart';
import 'package:share_fit/generated/locales.g.dart';
import 'sports_state.dart';

class SportsLogic extends BaseSportsController implements DeviceChangeListener {
  final SportsState state = SportsState();

  GoogleMapController? googleMapController;

  late TabController tabController;

  @override
  String getRemotePath() {
    return UTEPedConst.pathSports;
  }

  @override
  String getStoragePath() {
    return UTEPedConst.pathSports;
  }

  @override
  void onInit() {
    UTEPedDevice.instance.addListener(this);
    super.onInit();
  }

  @override
  void initState() {
    // List<ExerciseType> mySportList = read(SportsConst.mySportList, defaultValue: <ExerciseType>[]);
    // if (mySportList.isNotEmpty) {
    //   state.sportList = mySportList;
    // }
    //
    // updateTabBar();

    tabController = TabController(length: state.sportList.length, vsync: this);

    update(['sport_list']);
  }

  @override
  void onReady() {
    super.onReady();
    uTEPedSports.getDeviceOperatorState();
    // 请求定位权限
    uTEPedSports.checkLocationPermission();
  }

  @override
  void onRemoteEvent(event, method) {
    Map map = event[UTEPedConst.keyValue];
    if (method == UTEPedConst.methodQueryWorkoutManageInfo) {
      // Set set = Set.from(map[UTEPedConst.typeList] ?? []);
      // state.sportList = set.isEmpty ? state.sportList : set.map((type) => ExerciseType(Type.getType(type), '')).toList();
      // save(SportsConst.mySportList, state.sportList);
      // updateTabBar();
      // update(['sport_list']);
    } else if (method == UTEPedConst.methodSetSportOneRecord) {
      // 手表上报的运动数据
      // uTEPedSports.onSaveExerciseRecord(map, state.polyLine.points, (wm) => saveNecessaryParams(wm));
    }
  }

  @override
  void onDeviceChangeEvent(event) {
    String action = event[UTEPedConst.keyAction];
    if (action == UTEPedConst.actionConnect) {
      // queryWorkOutManageInfo();
    }
  }

  void selectSport(int index) {
    if (state.index != index) {
      state.index = index;
    }
  }

  @override
  void onClose() {
    super.onClose();
    googleMapController?.dispose();
    // tabController.dispose();
  }

  goGoalSettingPage() {
    Get.toNamed(SportsRoutes.goalSettingPage, arguments: state.sportList[state.index].type.code);
  }

  goCountDownPage() async {
    var connected = await UTEPedDevice.instance.isConnected();
    if (connected) {
      uTEPedSports.start(workoutType: state.sportList[state.index].type.code);
    }
    else {
      showTips(LocaleKeys.info.ts, LocaleKeys.device_not_connected.ts);
    }
  }

  onMapCreated(GoogleMapController controller) {
    googleMapController = controller;
    // 重新获取当前定位
    uTEPedSports.getCurrentLocation().then((pos) {
      controller.moveCamera(CameraUpdate.newLatLng(LatLng(pos.latitude, pos.longitude)));
    });
  }

  onAddSportItem() {
    Get.toNamed(SportsRoutes.sportListPage, arguments: state.sportList)?.then((list) {
      if (list != null) {
        state.sportList = list;
      }

      updateTabBar();
      update(['sport_list']);
    });
  }

  updateTabBar() {
    tabController = TabController(length: state.sportList.length, vsync: this);
    tabController.animateTo(0);

    state.index = 0;
  }
}
